<?php
/**
 * Conversations - A lightweight BB for Xaraya
 *
 * @package modules
 * @copyright (C) 2003-2006 The Digital Development Foundation.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage conversations
 * @link http://xaraya.com/index.php/release/760.html
 * @author John Cox
*/
/** 
 * View a tag
 * 
 * @package Xaraya eXtensible Management System
 * @copyright (C) 2005 by the Xaraya Development Team.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.org
 *
*/

/**
 * @author John Cox
 * @author jojodee
 * @param tagid integer
 */

function conversations_user_tag_rss()
{

    if (!xarVarFetch('tagid', 'str:1:', $tagid, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('startnum', 'id', $startnum, 1, XARVAR_NOT_REQUIRED)) return;
    
    // TODO
    // Post per page will be a user only setting.
    // Let's just do variable here
    
    $postperpage = 10;

    if (!xarSecurityCheck('Viewcon', 1)) return;

    $data = array();
    $taginfo = array();
    $posts = array();
    
    xarModAPIFunc('conversations', 'user', 'updatetag', array(
                                                          'tagid'   => $tagid,
                                                          'count'   => 1));
    
    // Let's get our tag information for the template first.    
    $taginfo = xarModAPIFunc('conversations', 'user', 'gettags', array(
                                                          'id'   => $tagid));
    $data['taginfo'] = $taginfo;
    
    
    // TODO
    // I probably should do a left join here.  It would cut down on this code and 
    // limit the queries that are made.
    // Screw it, done now...

    // Get ye old posts associated with the tags
    // First what are we getting?
    
    $posts = xarModAPIFunc('conversations', 'user', 'getposts_bytagid', array(
                                                          'tagid'   => $tagid,
                                                          'startnum'=> $startnum,
                                                          'numitems'=> $postperpage));
    
    // Well that was easy enough.  $posts now holds the array for just this tag
    // What kind of cool stuff do we really want to do with it?
    // I guess maybe transforms?  Anything else?  We probably also want to do 
    // page (dance) nation here as well.
    // We also need the user information.
    // Start the loop
    $data['items'] = array();
    $totalposts = count($posts);
    for ($i = 0; $i < $totalposts; $i++) {
        $post = $posts[$i];
        
        // Hold the not_transformed for modified, quoting.
        $posts[$i]['not_transformed'] = $post['text'];
        
        // Transform the output        
        $posts[$i]['text'] = xarModCallHooks('item', 'transform', 1, $post['text'], 'conversations', 2);
        
        $posts[$i]['tagname'] = $taginfo[0]['tag'];
        
        // Get User Information
        $posts[$i]['userinfo'] = xarModAPIFunc('roles', 'user', 'get', array(
                                                          'uid'   => $post['uid']));
        // Let's extract the cool stuff and then unset the array.   
        $posts[$i]['uname'] = $posts[$i]['userinfo']['uname'];
        $posts[$i]['name'] = $posts[$i]['userinfo']['name'];
        $posts[$i]['email'] = $posts[$i]['userinfo']['email'];
        $posts[$i]['date_reg'] = $posts[$i]['userinfo']['date_reg'];
        unset($posts[$i]['userinfo']);
        $posts[$i]['current_uid'] = xarUserGetVar('uid');
        // Let's get web2.0ie...
        if ($posts[$i]['current_uid'] == $post['uid']){
          $posts[$i]['class'] = 'con-my-class';
        } else {
          $posts[$i]['class'] = 'con-reg-class';
        }
    }
    
    $data['items'] = $posts;
    // Here is my array for the template now
    /*
        array (
          'taginfo' => 
          array (
            0 => 
            array (
              'id' => '2',
              'tag' => 'test2',
              'timestamp' => '1154641869',
              'status' => '1',
              'uid' => '3',
              'count' => '1',
              'options' => false,
            ),
          ),
          'items' => 
          array (
            0 => 
            array (
              'postid' => '2',
              'timestamp' => '1154641869',
              'uid' => '3',
              'text' => 'another test',
              'status' => '1',
              'count' => 'N;',
              'options' => false,
              'tagid' => '2',
              'uname' => 'Admin',
              'name' => 'Administrator',
              'email' => 'john.cox@wyome.com',
              'date_reg' => '1154117266',
            ),
          ),
    */
    
    // Might as well set the page title while we are here.
    xarTplSetPageTitle($taginfo[0]['tag']);
    return $data;
}

?>